package lectores.parsersXLSX;

import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class ProcesadorXLSX {

    private ContentHandler manejadorContenidoXML;
    private XSSFReader xssfReader;
            
    public ProcesadorXLSX(ContentHandler manejadorContenidoXML, XSSFReader xssfReader) {
        this.manejadorContenidoXML = manejadorContenidoXML;
        this.xssfReader = xssfReader;
    }

    public void process() throws IOException, SAXException, OpenXML4JException, ParserConfigurationException {
        XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
        InputStream primerHoja = iter.next();
        processSheet(primerHoja);
        primerHoja.close();
    }
    
    public void processSheet(InputStream sheetInputStream) throws IOException, ParserConfigurationException, SAXException {
        InputSource sheetSource = new InputSource(sheetInputStream);
        SAXParserFactory saxFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxFactory.newSAXParser();
        XMLReader sheetParser = saxParser.getXMLReader();
        sheetParser.setContentHandler(this.manejadorContenidoXML);
        sheetParser.parse(sheetSource);
    }
}
